﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;

public class IOSBuilderEditor : EditorWindow
{
    string[] selectGrid = null;
    private List<string> channelNames;
    private List<string> channelPaths;
    private int selectedIndex = 0;

    [MenuItem("Build/ManualOperationIOSBuild")]
    public static void OpenManualOperation()
    {
       var ins =  GetWindow<IOSBuilderEditor>("IOSChannel Builder");
        ins.GetList();
    }

    public void GetList()
    {
        channelNames = new List<string>();
        channelPaths = new List<string>();
        var cfgRoot = new DirectoryInfo(Application.dataPath +"/"+ XCodeProjectConfig.ChannelConfigFodlerEx);
        var subFolders = cfgRoot.GetDirectories();
        foreach (var item in subFolders)
        {
            channelPaths.Add(item.FullName);
            channelNames.Add(item.Name);
        }
        selectGrid = channelNames.ToArray();
    }

    void OnGUI()
    {
        GUILayout.BeginHorizontal();
        selectedIndex = GUILayout.SelectionGrid(selectedIndex, selectGrid, 1);
        if (GUILayout.Button("Build"))
        {
            BuildChannel();
        }
        GUILayout.EndHorizontal();
    }
    private void BuildChannel()
    {
        string selectPath = EditorUtility.SaveFolderPanel("Build XCode Project To...", XCEditorUtils.CombinePaths(Application.dataPath, "../"), null);
        if (!string.IsNullOrEmpty(selectPath))
        {
            string xcodeFolderName = "ios_" + channelNames[selectedIndex] + "_" + DateTime.Now.ToString("yyyyMMddHHmm");
            string targetPath = XCEditorUtils.CombinePaths(selectPath, xcodeFolderName);
            XCodeProjectMod.CurrentBuildChannelName = channelNames[selectedIndex];
            XCodeProjectBuilder.BuildXCodeProject(targetPath);
        }
    }
}
